package com.endoc.news.fragment.newslist;

import com.endoc.news.fragment.base.BaseMvvmModel;
import com.endoc.news.fragment.base.BaseObserver;
import com.endoc.news.fragment.base.BaseViewModel;
import com.endoc.news.fragment.mvvm.model.IBaseModelListener;
import com.endoc.news.fragment.mvvm.model.PagingResult;
import com.endoc.news.fragment.newslist.view.pictureview.PictureViewModel;
import com.endoc.news.fragment.newslist.view.titleview.TitleViewModel;
import com.endoc.news.netWoke.NetClient;
import com.endoc.news.netWoke.NewsApiInterface;
import com.endoc.news.netWoke.bean.NewsListBean;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewsListBeanModel extends BaseMvvmModel<NewsListBean,List<BaseViewModel>> {

    //IBaseModelListener<List<BaseViewModel>> mIBaseModelListener;
    String mChannelId;
    String mChannelName;
/*    public NewsListBeanModel(IBaseModelListener<List<BaseViewModel>> iBaseModelListener) {
        mIBaseModelListener = iBaseModelListener;
    }*/

    //int page = 1;

    public NewsListBeanModel(String channelId,
                             String channelName) {
        super(true, channelId+channelName+"_preference_key",null,1);
        //mPage = 1;
        mChannelId =channelId;
        mChannelName =channelName;
    }

 /*   @Override
    public void refresh() {

      //  Logger.d("刷新了.........");
        //mBaseViewModels.clear();
        //mPage = mInitPage;

        //load();
    }*/


    //此集合就是返回给fragment的数据
    //List<BaseViewModel> mBaseViewModels = new ArrayList<BaseViewModel>();

    @Override
    public void load() {
        NetClient.getInstance().getApiService(NewsApiInterface.class).getNewsList(mChannelId, mChannelName, mPage + "").compose(NetClient.getInstance().getObservableTransformer(new BaseObserver<NewsListBean>(this,this)));
      /*  NetClient.getInstance().getApiService(NewsApiInterface.class).getNewsList(mChannelId, mChannelName, mPage + "").compose(NetClient.getInstance().getObservableTransformer(new BaseObserver<NewsListBean>() {
            @Override
            public void success(NewsListBean newsListBean) {

                    Logger.d("page====" + mPage + "  " + newsListBean);
                    List<BaseViewModel> baseViewModels = new ArrayList<BaseViewModel>();
                    if (newsListBean.showapiResBody.retCode != 0) {
                        Logger.d("数据加载失败");
                    } else {
                        List<NewsListBean.Contentlist> contentlist = newsListBean.showapiResBody.pagebean.contentlist;
                        for (int i = 0; i < contentlist.size(); i++) {
                            if (contentlist.get(i).imageurls != null & contentlist.get(i).imageurls.size() > 0) {
                                Logger.d("getItemViewType");
                                PictureViewModel pictureViewModel = new PictureViewModel();
                                pictureViewModel.jumpUri = contentlist.get(i).link;
                                pictureViewModel.title = contentlist.get(i).title;
                                pictureViewModel.imageUrl = contentlist.get(i).imageurls.get(0).url;
                                //mBaseViewModels.add(pictureViewModel);
                                baseViewModels.add(pictureViewModel);
                                //return TYPE_HAVE_URL;
                            } else {
                                TitleViewModel titleViewModel = new TitleViewModel();
                                titleViewModel.jumpUri = contentlist.get(i).link;
                                titleViewModel.title = contentlist.get(i).title;
                                //mBaseViewModels.add(titleViewModel);
                                baseViewModels.add(titleViewModel);
                                //return TYPE_NO_URL;
                            }
                        }
                        //((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(mBaseViewModels);
                    }

                    //mIBaseModelListener.success(baseViewModels,new PagingResult(page==1,baseViewModels.size()==0,baseViewModels.size()>=10));
                    //weakReference.get().success(baseViewModels, new PagingResult(mPage == 1, baseViewModels.size() == 0, baseViewModels.size() >= 10));
                    notifyListener(newsListBean,baseViewModels,false);
                    //mPage++;
            }


            @Override
            public void fail(Throwable e) {
                loadFail(e);
            }
        }));*/
       /* NewsApiInterface apiService = NetClient.getInstance().getApiService(NewsApiInterface.class);
        apiService.getNewsList(mChannelId, mChannelName, mPage + "").subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NewsListBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NewsListBean newsListBean) {
                Logger.d("page====" + mPage + "  " + newsListBean);
                List<BaseViewModel> baseViewModels = new ArrayList<BaseViewModel>();
                if (newsListBean.showapiResBody.retCode != 0) {
                    Logger.d("数据加载失败");
                } else {
                    List<NewsListBean.Contentlist> contentlist = newsListBean.showapiResBody.pagebean.contentlist;
                    for (int i = 0; i < contentlist.size(); i++) {
                        if (contentlist.get(i).imageurls != null & contentlist.get(i).imageurls.size() > 0) {
                            Logger.d("getItemViewType");
                            PictureViewModel pictureViewModel = new PictureViewModel();
                            pictureViewModel.jumpUri = contentlist.get(i).link;
                            pictureViewModel.title = contentlist.get(i).title;
                            pictureViewModel.imageUrl = contentlist.get(i).imageurls.get(0).url;
                            //mBaseViewModels.add(pictureViewModel);
                            baseViewModels.add(pictureViewModel);
                            //return TYPE_HAVE_URL;
                        } else {
                            TitleViewModel titleViewModel = new TitleViewModel();
                            titleViewModel.jumpUri = contentlist.get(i).link;
                            titleViewModel.title = contentlist.get(i).title;
                            //mBaseViewModels.add(titleViewModel);
                            baseViewModels.add(titleViewModel);
                            //return TYPE_NO_URL;
                        }
                    }
                    //((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(mBaseViewModels);
                }

                //mIBaseModelListener.success(baseViewModels,new PagingResult(page==1,baseViewModels.size()==0,baseViewModels.size()>=10));
                //weakReference.get().success(baseViewModels, new PagingResult(mPage == 1, baseViewModels.size() == 0, baseViewModels.size() >= 10));
                notifyListener(newsListBean,baseViewModels);
                //mPage++;
            }

            @Override
            public void onError(Throwable e) {
                loadFail(e);
                //((IBaseModelListener)weakReference.get()).fail(e);
                //mIBaseModelListener.fail(e);
            }

            @Override
            public void onComplete() {

            }
        });*/
    }

    @Override
    public void onSuccess(NewsListBean newsListBean, boolean isFromCache) {
        Logger.d("page====" + mPage + "  " + newsListBean);
        List<BaseViewModel> baseViewModels = new ArrayList<BaseViewModel>();
        if (newsListBean.showapiResBody.retCode != 0) {
            Logger.d("数据加载失败");
        } else {
            List<NewsListBean.Contentlist> contentlist = newsListBean.showapiResBody.pagebean.contentlist;
            for (int i = 0; i < contentlist.size(); i++) {
                if (contentlist.get(i).imageurls != null & contentlist.get(i).imageurls.size() > 0) {
//                    Logger.d("getItemViewType");
                    PictureViewModel pictureViewModel = new PictureViewModel();
                    pictureViewModel.jumpUri = contentlist.get(i).link;
                    pictureViewModel.title = contentlist.get(i).title;
                    pictureViewModel.imageUrl = contentlist.get(i).imageurls.get(0).url;
                    //mBaseViewModels.add(pictureViewModel);
                    baseViewModels.add(pictureViewModel);
                    //return TYPE_HAVE_URL;
                } else {
                    TitleViewModel titleViewModel = new TitleViewModel();
                    titleViewModel.jumpUri = contentlist.get(i).link;
                    titleViewModel.title = contentlist.get(i).title;
                    //mBaseViewModels.add(titleViewModel);
                    baseViewModels.add(titleViewModel);
                    //return TYPE_NO_URL;
                }
            }
            //((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(mBaseViewModels);
        }

        //mIBaseModelListener.success(baseViewModels,new PagingResult(page==1,baseViewModels.size()==0,baseViewModels.size()>=10));
        //weakReference.get().success(baseViewModels, new PagingResult(mPage == 1, baseViewModels.size() == 0, baseViewModels.size() >= 10));
        notifyListener(newsListBean,baseViewModels,false);
    }

    @Override
    public void onFailure(Throwable e) {
        loadFail(e);
    }
}
